package com.sd.activity;

import java.io.File;
import java.net.URLDecoder;
import java.util.ArrayList;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alex.books.BaseBook;
import com.alex.http.AGetReuqest;
import com.alex.http.AHandledResult;
import com.alex.http.AHttpEngine;
import com.alex.http.AHttpListener;
import com.alex.log.ALog;
import com.mobclick.android.MobclickAgent;
import com.sd.activity.r.R;
import com.sd.reader.ReaderSet;
import com.sd.util.BookCabin;
import com.sd.util.DownloaderApk;
import com.sd.util.LoadingProgressDialog;
import com.sd.util.QequestHandleable;
import com.sd.view.DialogManager;
import com.sd.view.ToastManager;

public class SingleReadActivity extends BaseActivity implements View.OnClickListener
{
	private long REQUEST_VERSION = 0x01;
	private long REQUEST_DOWN = 0x02;
  private static final int REQUEST_EX = 1;
/*  AHttpListener httpListener = new AHttpListener(this)
  {
    public void onError(, Bundle paramBundle)
    {
      ALog.e("onError");
      LoadingProgressDialog.dismiss();
      ToastManager.getInstance().showText(this.this$0, "你已经是最新的版本了~~~");
    }

    public void onResult(, Bundle paramBundle, AHandledResult paramAHandledResult)
    {
      LoadingProgressDialog.dismiss();
      Intent localIntent = new Intent("android.intent.action.VIEW");
      String str = (String)paramAHandledResult.mObj;
      if (str != null)
      {
        localIntent.setDataAndType(Uri.fromFile(new File(str)), "application/vnd.android.package-archive");
        this.this$0.startActivity(localIntent);
      }
      while (true)
      {
        return;
        ToastManager.getInstance().showText(this.this$0, "你已经是最新的版本了~~~");
      }
    }
  };*/
  
  private BaseBook mBasebook;
  private ImageView mBookIB;
  private ImageView mBookUpdate1;
  private ImageView mBookUpdate2;
  private ImageView mBookUpdate3;
  private ImageView mBookUpdate4;
  private TextView titleTV;


  public void onBackPressed()
  {
    DialogManager.getInstance().createExitDialog(this);
  }

  @Override
  public void onClick(View v) {
  	// TODO Auto-generated method stub
	  int v_id = v.getId();
	  Intent localIntent = new Intent();
	  switch (v_id) {
	  	case R.id.bookupdate1:
	  	case R.id.bookupdate2:
	  	case R.id.bookupdate3:
	  	case R.id.bookupdate4:
	  		LoadingProgressDialog.show(this, R.string.msg_loading);
			String url = "http://version.4gshu.com/version?type=android&oldver=1.0.0";
            AGetReuqest get = new AGetReuqest(REQUEST_VERSION, url, httpListener);
            get.setHandleable(new QequestHandleable());
            AHttpEngine.getInstance().request(get);
		break;
	  	case R.id.singlebook:
	  		localIntent.putExtra(ReadBookActivity.BOOK_ID, this.mBasebook.mBookId);
            localIntent.setClass(this, ReadBookActivity.class);
            startActivity(localIntent);
	  		break;
		default:
			break;
	}
  }
  /*public void onClick(View paramView)
  {
    int i = paramView.getId();
    Intent localIntent = new Intent();
    switch (i)
    {
    default:
    case 2131165282:
    case 2131165283:
    case 2131165284:
    case 2131165285:
    case 2131165280:
    case 2131165333:
    case 2131165334:
    case 2131165335:
    }
    while (true)
    {
      while (true)
      {
        while (true)
        {
          while (true)
          {
            while (true)
            {
              return;
              LoadingProgressDialog.show(this, 2131296266);
              AGetReuqest localAGetReuqest = new AGetReuqest(90L, "http://221.181.69.73/turntest.apk", this.httpListener);
              localAGetReuqest.setHandleable(new DownloaderApk());
              AHttpEngine.getInstance().request(localAGetReuqest);
            }
            localIntent.putExtra(ReadBookActivity.BOOK_ID, this.mBasebook.mBookId);
            localIntent.setClass(this, ReadBookActivity.class);
            startActivity(localIntent);
          }
          int l = ((Integer)paramView.getTag()).intValue();
          DialogManager.getInstance().dismiss();
          startActivity(BookInfoActivity.getBookMark(this, l));
        }
        int k = ((Integer)paramView.getTag()).intValue();
        DialogManager.getInstance().dismiss();
        startActivity(BookInfoActivity.getBookInfo(this, k));
      }
      int j = ((Integer)paramView.getTag()).intValue();
      DialogManager.getInstance().dismiss();
      startActivity(BookInfoActivity.getBookContents(this, j));
    }
  }
*/
  public void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    requestWindowFeature(1);
    setContentView(2130903059);
    this.mBasebook = ((BaseBook)BookCabin.getInstance().getBooks(this, "").get(0));
    this.titleTV = ((TextView)findViewById(R.id.title));
    this.titleTV.setText(this.mBasebook.mBookName);
    ReaderSet.getInstance().init(this);
    this.mBookIB = ((ImageView)findViewById(R.id.singlebook));
    this.mBookIB.setOnClickListener(this);
    this.mBookUpdate1 = ((ImageView)findViewById(R.id.bookupdate1));
    this.mBookUpdate1.setOnClickListener(this);
    this.mBookUpdate2 = ((ImageView)findViewById(R.id.bookupdate2));
    this.mBookUpdate2.setOnClickListener(this);
    this.mBookUpdate3 = ((ImageView)findViewById(R.id.bookupdate3));
    this.mBookUpdate3.setOnClickListener(this);
    this.mBookUpdate4 = ((ImageView)findViewById(R.id.bookupdate4));
    this.mBookUpdate4.setOnClickListener(this);
    MobclickAgent.setAutoLocation(false);
    MobclickAgent.setUpdateOnlyWifi(false);
    MobclickAgent.update(this);
    MobclickAgent.onError(this);
    MobclickAgent.updateOnlineConfig(this);
  }

  protected void onResume()
  {
    super.onResume();
  }

  protected void onStart()
  {
    super.onStart();
  }

  AHttpListener httpListener =new AHttpListener() {
		
		@Override
		public void onResult(long requestId, Bundle paramBundle,
				AHandledResult paramHandledResult) {
			// TODO Auto-generated method stub
			LoadingProgressDialog.dismiss();
			if(requestId == REQUEST_VERSION){
				Log.e("REQUEST_VERSION", "");
				ArrayList<String> list = (ArrayList<String>) paramHandledResult.mResults;
				String code = list.get(0);
				String url = list.get(1);
				if(code!=null && url != null && code.equals("1")){
					Log.e("REQUEST_VERSION", ""+url);
					url = URLDecoder.decode(url);
					LoadingProgressDialog.show(SingleReadActivity.this, R.string.msg_loading);
					AGetReuqest get = new AGetReuqest(REQUEST_DOWN, url, httpListener);
		            get.setHandleable(new DownloaderApk());
		            AHttpEngine.getInstance().request(get);
				}else{
					ToastManager.getInstance().showText(SingleReadActivity.this, "你已经是最新的版本了~~~");
				}
	            
			}else if(requestId == REQUEST_DOWN){
				LoadingProgressDialog.dismiss();
				Log.e("REQUEST_DOWN", "");
				Intent intent = new Intent(Intent.ACTION_VIEW);
				String apkName = (String)paramHandledResult.mObj;
				if(apkName!=null){
					File file = new File(apkName);
					intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
					SingleReadActivity.this.startActivity(intent);
				}else{
					ToastManager.getInstance().showText(SingleReadActivity.this, "你已经是最新的版本了~~~");
				}
			}
		}
		
		@Override
		public void onError(long requestId, Bundle paramBundle) {
			// TODO Auto-generated method stub
			ALog.e("onError");
			LoadingProgressDialog.dismiss();
			if(requestId == REQUEST_VERSION){
				ToastManager.getInstance().showText(SingleReadActivity.this, "你已经是最新的版本了~~~");
			}else if(requestId == REQUEST_DOWN){
				ToastManager.getInstance().showText(SingleReadActivity.this, "你已经是最新的版本了~~~");
			}
		}
	};


}

